﻿using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BLL;
using System.Text;


public partial class Plublic_BankTransfer : System.Web.UI.Page
{
    String StrConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    CardsHandler objCardsHandler = new CardsHandler();
    TransactionHandler objTransactionHandler = new TransactionHandler();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindDropDowList();

        }
        
    }

    protected void BindDropDowList() 
    {
        int UserId = Convert.ToInt32(Session["UserId"]);
        Cards objCards = new Cards();

        DataTable dt =  objCardsHandler.GetCardsTable(UserId, StrConn);

        ddlTranferAccount.DataSource = dt;
        ddlTranferAccount.DataValueField = "AccountNumber";
        ddlTranferAccount.DataBind();

    }
    

    protected void btnGetInfo_Click(object sender, System.EventArgs e)
    {
        string AccountReceived = txtReceivedAccount.Text.ToString();
        DataTable dt = objCardsHandler.GetCardsTableReceived(AccountReceived, StrConn);

        if(dt.Rows.Count ==1)
        {
            DataRow row = dt.Rows[0];

            txtNameReceived.Text = row["Username"].ToString();
            txtCurrencyReceived.Text = row["currence"].ToString();

        }
    }
    protected void btnTransfer_Click(object sender, System.EventArgs e)
    {
        string AccTransfer = ddlTranferAccount.SelectedItem.ToString();
        string AccReceived = txtReceivedAccount.Text.ToString();
        double AmountTransfer = Convert.ToDouble(txtAmountTransfer.Text);
        double BalanceTransfer = Convert.ToDouble(txtBalanceTransfer.Text);
        Transaction objTransaction = new Transaction();

        objTransaction.Created = DateTime.Now.ToString();
        objTransaction.Updated = DateTime.Now.ToString();
        objTransaction.AccountNumber = AccTransfer;
        objTransaction.AccountNumberTo =AccReceived;
        objTransaction.Balance = BalanceTransfer;
        objTransaction.AmountTransaction = AmountTransfer;
        objTransaction.Currency = txtCurrencyTransfer.Text.ToString();
        objTransaction.LocationID = 1;
        objTransaction.Descriptions = txtDescription.Text.ToString();
        objTransaction.Status = "Active";
        objTransaction.Acction = "Transfer";
        objTransaction.ErrorMessage = "";

        if (AmountTransfer < BalanceTransfer)
        {
            objCardsHandler.UpdateBalanceReceived(AccReceived, AmountTransfer, StrConn);
            objCardsHandler.UpdateBalanceTransfer(AccTransfer, AmountTransfer, StrConn);
            objTransactionHandler.AddNewTransaction(objTransaction, StrConn);
            this.setText();
        }
        
    }
    protected void setText()
    {
        txtReceivedAccount.Text = "";
        txtAmountTransfer.Text = "";
        txtCurrencyReceived.Text = "";
        txtNameReceived.Text = "";
        txtDescription.Text = "";

    }

    protected void ddlTranferAccount_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        String TransferAcc = ddlTranferAccount.SelectedItem.ToString();

        if (TransferAcc != string.Empty)
        {
            Cards objCards = new Cards();
            objCards = objCardsHandler.GetCardsDetailsTransfer(TransferAcc, StrConn);

            if (objCards != null)
            {
                txtBalanceTransfer.Text = objCards.Balance.ToString();

                txtCurrencyTransfer.Text = objCards.Currency.ToString();
            }

        }
    }
    protected void ddlTranferAccount_Load(object sender, System.EventArgs e)
    {
        String TransferAcc = ddlTranferAccount.Text.ToString();

        if (TransferAcc != string.Empty)
        {
            Cards objCards = new Cards();
            objCards = objCardsHandler.GetCardsDetailsTransfer(TransferAcc, StrConn);

            if (objCards != null)
            {
                txtBalanceTransfer.Text = objCards.Balance.ToString();

                txtCurrencyTransfer.Text = objCards.Currency.ToString();
            }

        }
    }
}